#include "chatcommand_simplegamesetdata.h"
#include "websocket.h"
#include "datastring.h"
#include "parameters.h"
#include "stringbuilder.h"
#include "message.h"
#include "Debug.h"
#include <inttypes.h>
#include "simplechatgame.h"

// simplegamesetdata(messageid,gameid,offset,newgamedata)
// Sets the gamedata in a game. Sends a simplegamedata message or an error. Offset is where to save newgamedata within gamedata. Use -1 to replace.

bool chatcommand_simplegamesetdata::processmessage(char first_letter,message *received_message,chatclient *client)
{
	if ((first_letter != 's') 
		|| (received_message->actual_message.substr(0,18)!="simplegamesetdata(")) {
		return false;
	}	
	
	Debug debug(__FILE__,__func__,__LINE__);
	message *new_message;
	datastring method_parameters;
	parameters parameters_parsed;
	bool parameter_success = true;
	int64_t messageid;
	datastring gameid;
	int64_t offset;
	datastring newgamedata;
	simplechatgame *game;
	biglist_item<simplechatgame *> *gamesearch;
	datastring error_message;
	datastring *username;	
	biglist_item<simplechatgameuser *> *usersearch;
	datastring currentgamedata;
	
	debug = __LINE__;
	method_parameters = received_message->actual_message.substr(18,received_message->actual_message.length-19);
	method_parameters.null_terminate(); // Used to comply with snprintf.
	parameters_parsed.long_parameter(method_parameters,parameter_success);
	parameters_parsed.string_parameter(method_parameters,parameter_success);
	parameters_parsed.long_parameter(method_parameters,parameter_success);
	parameters_parsed.string_parameter(method_parameters,parameter_success);
		
	debug = __LINE__;
	if (parameter_success) {
		// Give the parameters nice names.
		debug = __LINE__;
		messageid = parameters_parsed.long_parameters[0];
		gameid = parameters_parsed.string_parameters[1];
		offset = parameters_parsed.long_parameters[2];		
		newgamedata = parameters_parsed.string_parameters[3];
	
		debug = __LINE__;
		// Look to see if this gameid is already used.
		gamesearch = the_websocket->find_simple_game(gameid);
		if (gamesearch == nullptr) {
			error_message = "That gameid was not found.";
			error(client,error_message,messageid);
			return true;
		}
		game = gamesearch->item;
		// Look for this client in this game.
		usersearch = game->getuser(client);
		if (usersearch == nullptr) {
			error_message = "You're not in that game.";
			error(client,error_message,messageid);
			return true;
		}
		username = usersearch->item->username;
		// Set the gamedata. If offset < 0 then replace game data. If offset >= 0 and newgamedata.length > 0 then set part of gamedata based on offset.
		debug = __LINE__;
		if (offset < 0) {
			if (newgamedata.length > 0) {
				if (game->gamedata == nullptr) {
					game->gamedata = new datablock(newgamedata);
				} else {
					*(game->gamedata) = newgamedata;
				}
			} else {
				idisposable::dereference((idisposable**)&(game->gamedata));
			}
		} else {
			if (newgamedata.length > 0) {
				if (game->gamedata == nullptr) {
					game->gamedata = new datablock(newgamedata);
				} else {
					game->gamedata->alter(newgamedata,offset);
				}
			}
		}
		debug = __LINE__;
		currentgamedata.clear();
		if (game->gamedata != nullptr) {
			currentgamedata = *(game->gamedata);
		}
		// Send a message to all the clients that the gamedata has changed and who changed it.
		new_message = simplegamedata(messageid,gameid,*username,currentgamedata);
		game->send_message_to_clients(new_message,nullptr);
		idisposable::dereference((idisposable**)&new_message);		
	} else {
		debug = __LINE__;
		parameters_not_correct(client,received_message);
	}
	debug = __LINE__;
	return true;
}
